package com.qianfeng.smartdevice.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.pojo.Area;
import com.qianfeng.smartdevice.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Created by jackiechan on 2020/12/13 10:18
 *
 * @Author jackiechan
 */
@RestController
@RequestMapping("/areas")
@Api(tags = "区域管理模块" )//在接口文档的 controller 名字前面显示
public class AreaController {
    @Autowired
    private AreaService areaService;

    @ApiOperation("添加区域")
    @PostMapping("/area")
    public R addArea(@RequestBody Area area) {
        areaService.addArea(area);
        return R.setOK(null);
    }

    @PutMapping("/area")
    @ApiOperation("更新区域")
    public R updateArea(@RequestBody Area area) {
        areaService.updateArea(area);
        return R.setOK(null);
    }

    @DeleteMapping("/area")
    @ApiOperation("根据 id 删除区域")
    @ApiImplicitParam(name = "ids",value = "主键",required = true)
    public R deleteAreas(@RequestParam String[] ids) {
        areaService.deleteAreas(Arrays.asList(ids));
        return R.setOK(null);
    }

    @GetMapping("/area/{id}")
    @ApiOperation("根据 id 查询区域")
    public R getAreaById(@ApiParam(name = "id",value = "主键",defaultValue = "1") @PathVariable Long id) {

        Area area = areaService.getAreaById(id);
        return R.setOK(area);
    }

    @GetMapping("/area/parent/{id}")
    @ApiOperation("根据 父id 查询子区域")
    public R getAreaByParentId(@ApiParam(name = "id",value = "父id") @PathVariable Long id) {
        List<Area> areaList = areaService.getAreaByParentId(id);
        return R.setOK(areaList);
    }
    @GetMapping("/areas")
    @ApiOperation("查询所有区域")
    public R getAllAreas(@ApiParam(name = "page",value = "第几页",defaultValue = "1") @RequestParam(defaultValue = "1") int page,@ApiParam(name = "limit",value = "每页数量",defaultValue = "10") @RequestParam(defaultValue = "10") int limit,@ApiParam(name = "exludeId",value = "被忽略的区域 id") @RequestParam(required = false) String exludeId, @ApiParam(name = "areanname",value = "区域模糊名字")@RequestParam(required = false)  String areanname,@ApiParam(name = "status",value = "状态",defaultValue = "-100") @RequestParam(defaultValue = "-100") int status) {
        PageInfo<Area> pageInfo = areaService.getAllAreas(page , limit, exludeId, areanname, status);
        return R.setOK(pageInfo);
    }

    @GetMapping("/areas/parent")
    @ApiOperation("查询所有父区域")
    public R getAllFirstParentAreas(@ApiParam(name = "status",value = "状态",defaultValue = "-100") @RequestParam(defaultValue = "-100") String status) {
        List<Area> areaList = areaService.getAllFirstParentAreas(status);
        return R.setOK(areaList);
    }

}
